// JavaScript Document
STTweenOption = function()
{
	//this.css;
	this.top;
	this.right;
	this.bottom;
	this.left;
	
	this.ease = STEaseLite.easeInQuad;
	this.onComplete;
}

var STTween = function(target, duration, option)
{
	var _p = this._p_ = {};
	_p.target = target;
	_p.duration = duration;
	_p.option = option;
	
	// 값 초기화
	_p.tick = 0;
	_p.maxTick = parseInt( STICKER.FPS*duration );
	if( _p.option != undefined )
	{
		_p.initialValues = {};
		_p.offsetValues = {};
		
		var _cv, _d;
		var _k2;
		for( var key in _p.option )
		{
			if( key == "top" || key == "right" || key == "bottom" || key == "left" )
			{
				if( _p.target.style[ key ] == "" )
				{
					// 정의되어 있지 않으면 offset 값에서 끌어옴
					_k2 = "offset"+key.substr(0,1).toUpperCase()+key.substr(1);
					_p.target.style[ key ] = _p.target[ _k2 ]+"px";
				}
				_p.initialValues[ key ] 	= parseFloat( _p.target.style[ key ] );
				_p.offsetValues[ key ]		= _p.option[ key ] - _p.initialValues[ key ];
			}
		}
	}
}

// [ Static 및 루프를 위함 ]
STTween.__private__ = {};
STTween.__private__.queue = [];
STTween.__private__.searchForTarget = function( target )
{
	var _tween;
	for( var i in this.queue )
	{
		_tween = this.queue[i];
		if( _tween._p_.target == target )
		{
			return _tween;
		}
	}
	return null;
}
STTween.__private__.intervalID = -1;
STTween.__private__.updateStart = function()
{
	if( this.intervalID == -1 && this.queue.length > 0 )
	{
		STICKER.log( "STTween.__private__.updateStart" );
		this.intervalID = setInterval( STTween.__private__.update, STICKER.RENDER_INTERVAL );
	}
}
STTween.__private__.update = function()
{
	var _tween;
	for( var i in STTween.__private__.queue )
	{
		_tween = STTween.__private__.queue[i];
		_tween.update();
	}
}
STTween.__private__.updateStop = function()
{
	if( this.queue.length == 0 && this.intervalID != -1 )
	{
		clearInterval( this.intervalID );
		this.intervalID = -1;
		
		STICKER.log( "STTween.__private__.updateStop" );
	}
}



// [ ProtoType ]
STTween.prototype = 
{
	start : function()
	{
		STICKER.log("STTween . start" );
		var _q = STTween.__private__.queue;
		if( _q.indexOf( this ) < 0 )
		{
			_q.push( this );
			STICKER.log("STTween . start . push");
			
			STTween.__private__.updateStart();
		}
	},
	
	finish : function( callComplete )
	{
		var _q = STTween.__private__.queue;
		var _idx = _q.indexOf( this );
		this._p_.maxTick = 0;	// 종료flag
		STICKER.log( "STTween . finish", _idx );
		if( _idx > -1 )
		{
			STICKER.log( "STTween . finish . splice" );
			_q.splice( _idx, 1 );
			
			if( callComplete && this._p_.option && this._p_.option.onComplete )
			{
				STICKER.log( "STTween . finish . onComplete()" );
				this._p_.option.onComplete();
			}
			
			STTween.__private__.updateStop();
		}
	},
	
	update : function()
	{
		var _ct = this._p_.tick;
		var _mt = this._p_.maxTick;
		if( _mt == 0 ) return;			// 종료 flag
		
		var _finish = true;
		
		if( this._p_.option != undefined )
		{
			var _tv, _d;
			var _k2;
			var _ivalue, _offset;
			
			for( var key in this._p_.option )
			{
				if( key == "top" || key == "right" || key == "bottom" || key == "left" )
				{
					_ivalue = this._p_.initialValues[ key ];
					_offset	= this._p_.offsetValues[ key ];
					
					if( _ct == _mt )
					{
						_tv = this._p_.option[ key ];
					}else{
						_tv = this._p_.option.ease( null, _ct, _ivalue, _offset, _mt );
						_finish = false;	// 하나라도 업데이트가 이루어 질경우 tick 에 맞김
					}
					this._p_.target.style[ key ]	= ( _tv )+"px";
				}
			}
			
		}
		if( _finish )
		{
			this.finish( true );
			return;
		}
		this._p_.tick++;
	}
}


// API
STTween.clear = function( target, callComplete )
{
	var _tween = STTween.__private__.searchForTarget( target );
	if( _tween != null )
	{
		_tween.finish( callComplete );
	}
}
STTween.to = function( target, duration, option )
{
	var _tween = new STTween( target, duration, option );
	_tween.start();
}